﻿package mbalib.docviewer
{
	import flash.display.*;
	import flash.events.*;
	
	public class Reader extends Sprite
	{
		private var _docURL:String;
		private const BG_COLOR:int = 9605778;
		private var _normalPageNum:int;
		private var _toolBar:ToolBar;
		private var _bg:Sprite;
		private var _docViewer:DocViewer;
		private const BORDER_COLOR:int = 3355443;
		private var _firstPagesNum:int;
		private var _loading:Loading;
		
		public function Reader()
		{
			return;
		}// end function
		
		 
		
		public function get npn() : int
		{
			return this._normalPageNum;
		}// end function
		
		private function doFullScreen(event:FullScreenEvent = null) : void
		{
			if (event.fullScreen)
			{
				this._toolBar.reArrange("TO_FULL");
				//this._docViewer.switchDisplayState("TO_FULL");
			}
			else
			{
				this._toolBar.reArrange("TO_NORMAL");
				//this._docViewer.switchDisplayState("TO_NORMAL");
			}
			this.addBG();
			return;
		}// end function
		
		private function docPageLoaded(event:Event = null) : void
		{
			this._toolBar.pagesAll = this._docViewer.pagesAll;
			this._toolBar.pagesLoaded = this._docViewer.pagesLoaded;
			this._toolBar.pageth = this._docViewer.pageth;
			return;
		}// end function
		
		private function toolBarNormalBtnClick(event:Event = null) : void
		{
			stage.displayState = "normal";
			return;
		}
		
		public function set fpn(param1:int) : void
		{
			this._firstPagesNum = param1;
			return;
		}
		
		private function init() : void
		{
			stage.addEventListener(FullScreenEvent.FULL_SCREEN, this.doFullScreen);
			this.clearInterface();
			this.addBG();
			this.addToolBar();
			this.addDocViewer();
			return;
		}
		
		private function showLoading(event:Event = null) : void
		{
			if (!this._loading)
			{
				this._loading = new Loading();
			}
			addChild(this._loading);
			this._loading.x = stage.stageWidth / 2;
			this._loading.y = stage.stageHeight / 2;
			var _loc_2:* = this._docViewer.loadPercent;
			if (!_loc_2 || isNaN(_loc_2) || !isFinite(_loc_2))
			{
				_loc_2 = 0.01;
			}
			this._loading.loadBar.scaleX = _loc_2;
			return;
		}
		
		public function set npn(param1:int) : void
		{
			this._normalPageNum = param1;
			return;
		}	
		private function toolBarNextBtnClick(event:Event = null) : void
		{
			 this._docViewer.pageth ++;
			return;
		}
		private function toolBarPagethChanged(event:Event = null) : void
		{
			this._docViewer.pageth = this._toolBar.pageth;
			return;
		}
		
		public function isLoaded(param1:int) : Boolean
		{
			var _loc_2:Boolean = true;
			var _loc_3:* = this._docViewer.pagesLoadedArray;
			var _loc_4:* = param1;
			var _loc_5:* = Math.min(param1 + 3, _loc_3.length);
			while (_loc_4 < _loc_5)
			{
				
				if (!_loc_3[_loc_4])
				{
					_loc_2 = false;
					break;
				}
				_loc_4 = _loc_4 + 1;
			}
			return _loc_2;
		}
		
		private function hideLoading(event:Event = null) : void
		{
			if (this._loading)
			{
				if (contains(this._loading))
				{
					removeChild(this._loading);
				}
				this._loading = null;
			}
			return;
		}
		
		private function addToolBar() : void
		{
			if (!this._toolBar)
			{
				this._toolBar = new ToolBar();
			}
			this._toolBar.x = 0;
			this._toolBar.y = 0;
			addChild(this._toolBar);
			this._toolBar.addEventListener("TO_FULL", this.toolBarFullBtnClick);
			this._toolBar.addEventListener("TO_NORMAL", this.toolBarNormalBtnClick);
			this._toolBar.addEventListener("TO_SINGLE", this.toolBarSingleBtnClick);
			this._toolBar.addEventListener("TO_DOUBLE", this.toolBarDoubleBtnClick);
			this._toolBar.addEventListener("TO_ZOOM", this.toolBarZoomBtnClick);
			this._toolBar.addEventListener("PAGETH_CHANGED", this.toolBarPagethChanged);
			this._toolBar.init();
			return;
		}
		
	 
		
		public function get fpn() : int
		{
			return this._firstPagesNum;
		}
		
		public function get position() : Object
		{
			var _loc_1:* = this._docViewer.size["contentWidth"];
			 
			var _loc_2:* = Math.max((stage.stageWidth - _loc_1) / 2, 0);
			var _loc_3:* = stage.stageHeight;
			return {contentWidth:_loc_1, contentX:_loc_2, contentHeight:_loc_3};
		}
		
		public function set docURL(param1:String) : void
		{
			if (param1 != this._docURL)
			{
				this._docURL = param1;
				trace(_docURL);
				this.init();
			}
			return;
		} 		 
		
		private function toolBarPrevBtnClick(event:Event = null) : void
		{
			 this._docViewer.pageth --;
			return;
		}
		
		private function docViewPagethChanged(event:Event = null) : void
		{
			this._toolBar.pageth = this._docViewer.pageth;
			return;
		}
		
		private function addBG() : void
		{
			if (!this._bg)
			{
				this._bg = new Sprite();
			}
			var _loc_1:* = this._bg.graphics;
			_loc_1.clear();
			_loc_1.lineStyle(1, this.BORDER_COLOR);
			_loc_1.beginFill(this.BG_COLOR);
			_loc_1.drawRect(0, 0, (stage.stageWidth - 1), (stage.stageHeight - 1));
			_loc_1.drawRect(0, stage.stageHeight - 8, (stage.stageWidth - 1), 7);
			_loc_1.lineStyle(1, 11585251);
			_loc_1.drawRect(1, stage.stageHeight - 7, stage.stageWidth - 3.5, 4.5);
			_loc_1.endFill();
			this._bg.x = 0.5;
			this._bg.y = 0.5;
			addChildAt(this._bg, 0);
			return;
		}
		
		private function toolBarFullBtnClick(event:Event = null) : void
		{
			stage.displayState = "fullScreen";
			return;
		}
		
	 
		
		private function toolBarZoomBtnClick(event:Event = null) : void
		{
			this._docViewer.zoom = this._toolBar.zoom;
			return;
		}
		
		private function clearInterface() : void
		{
			while (numChildren)
			{
				
				removeChildAt(0);
			}
			return;
		}
		
		private function addDocViewer() : void
		{
			if (!this._docViewer)
			{
				this._docViewer = new DocViewer();
			}
			this._docViewer.x = 0;
			this._docViewer.y = 40;
			addChildAt(this._docViewer, getChildIndex(this._toolBar));
			this._docViewer.addEventListener("SHOW_LOADING", this.showLoading);//是否在加载中
			this._docViewer.addEventListener("STOP_LOADING", this.hideLoading);
			this._docViewer.addEventListener("PAGE_LOADED", this.docPageLoaded);
			this._docViewer.addEventListener("PAGETH_CHANGED", this.docViewPagethChanged);
			try
			{
				this._docViewer.fpn = this._firstPagesNum;//第一页
				this._docViewer.npn = this._normalPageNum;
				 this._docViewer.docURL = this._docURL;
			}
			catch (e:Error)
			{
			}
			return;
		}
		
		private function toolBarSingleBtnClick(event:Event = null) : void
		{
			this._docViewer.switchPagesMode("fullSingle");
			return;
		}
		
		private function toolBarDoubleBtnClick(event:Event = null) : void
		{
			this._docViewer.switchPagesMode("fullDouble");
			return;
		}
		
	}
}
